package com.gxsyh.nantie.phone.regist.management.provider.controller;

import com.gxsyh.nantie.commons.dto.ResponseResult;
import com.gxsyh.nantie.commons.utils.OkHttpClientUtil;
import com.gxsyh.nantie.phone.regist.management.provider.controller.dto.RegistParam;
import com.gxsyh.nantie.phone.regist.management.provider.domain.TbDepartment;
import com.gxsyh.nantie.phone.regist.management.provider.domain.TbDepartmentUser;
import com.gxsyh.nantie.phone.regist.management.provider.domain.TbUser;
import com.gxsyh.nantie.phone.regist.management.provider.mapper.TbDepartmentUserMapper;
import com.gxsyh.nantie.phone.regist.management.provider.mapper.TbUserMapper;
import com.gxsyh.nantie.phone.regist.management.provider.service.TbDepartmentService;
import com.gxsyh.nantie.phone.regist.management.provider.service.TbUserService;
import org.springframework.beans.factory.annotation.Value;
import okhttp3.ResponseBody;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Author hua96
 * @Date 2020-02-24
 * @Time 14:10
 * @Version 1.0.0
 */
@Transactional
@RestController
public class RegistController {

    @Resource
    TbDepartmentService tbDepartmentService;
    @Resource
    TbUserMapper tbUserMapper;
    @Resource
    TbDepartmentUserMapper tbDepartmentUserMapper;
    @Resource
    BCryptPasswordEncoder passwordEncoder;
    @Resource
    TbUserService tbUserService;
    @Value("${base.config.leaf.url}")
    String LEAF_URL;
    /**
     * 默认密码6个6
     */
    private static final String defaultPassword = "666666";

    /**
     * 通过父级ID查询所有下级部门
     */
    @GetMapping(value = "/profile/search/next")
    public ResponseResult<List<TbDepartment>> searchNext(@RequestParam Long parentId){
        List<TbDepartment> tbDepartments = tbDepartmentService.selectSubsetDepartment(parentId);
        //System.out.println(tbDepartments);
        return new ResponseResult<>(200,"查询成功",tbDepartments);
    }

    @PostMapping("/profile/postForm")
    public ResponseResult<Void> postForm(@RequestBody RegistParam registerParam) throws IOException {
        //使用OkHttp3.ResponseBody  获取雪花算法生成的id
        String respBody = OkHttpClientUtil.getInstance().getData(LEAF_URL+"register").body().string();

        if (registerParam != null){
            TbUser tbUser = new TbUser();
            tbUser.setId(Long.valueOf(respBody));
            tbUser.setName(registerParam.getName());
            tbUser.setUsername(registerParam.getName());
            tbUser.setJobNumber(registerParam.getJobNumber());
            tbUser.setPassword(passwordEncoder.encode(defaultPassword));
            tbUser.setUserStatus(0);
            tbUser.setCreated(new Date());
            tbUser.setUpdated(new Date());
            int userInsert = tbUserMapper.insert(tbUser);

            TbDepartmentUser tbDepartmentUser = new TbDepartmentUser();
            tbDepartmentUser.setUserId(Long.valueOf(respBody));
            tbDepartmentUser.setDepartmentId(registerParam.getDepartmentId());
            int insertDU = tbDepartmentUserMapper.insert(tbDepartmentUser);

            if (userInsert >0 && insertDU>0){
                return new ResponseResult<>(200,"申请成功，等待审核",null);
            }else {
                return new ResponseResult<>(400,"申请失败，请与管理员联系",null);
            }
        }
        return new ResponseResult<>(400,"申请失败,请认真填写",null);
    }

    @PostMapping("/profile/allow")
    public ResponseResult<Void> allowUser(@RequestParam Long id){
        tbUserService.updateUserState(id);
        return new ResponseResult<>(200,"成功启用用户",null);
    }

}
